#coding=utf-8

s1 = "一个句子"
s2 = "另一个句子"
s3 = "一个好人"
text = '''我有一个梦想
我打了他一下
这么多句子了怎么还不会阿
我们来分析一下这到底怎么回事
每个人都会有自己的梦想
自己的事情自己做
你能理解这个句子么'''
p2 = '自己有一个梦想'
p3 = '我能理解这是怎么回事'

p1 = "另一个好人".decode('utf-8')

u1 = s1.decode('utf-8')
u2 = s2.decode('utf-8')
u3 = s3.decode('utf-8')

words = {}

def learn_s(s):
    len_s = len(s)
    for i in range(len_s):
        # 添加新字
        w = s[i]
        if w not in words.keys():
            words[w] = {}

        w_obj = words[w]
        # 获取下一个字（对双字词处理)
        if i == len_s - 1:
            n_w = 0
        else:
            n_w = s[i+1]
        
        # 计算频度
        if n_w not in w_obj.keys():
            w_obj[n_w] = 1
        else:
            w_obj[n_w] += 1

def cut_s(s):
    
    orig_words = []
    len_s = len(s)
    word_list = []

    out_list = []
    val_list = []
    
    # 初步切词，计算单个词出现频率
    for i in range(len_s):
        w1 = s[i]
        if i == len_s - 1:
            w2 = 0
        else:
            w2 = s[i+1]

        val = 0
        if w1 in words.keys():
            if w2 in words[w1].keys():
                val = words[w1][w2]
                
        piece = (w1, w2)
        val_list.append(val)
        word_list.append(piece)         

    if len(word_list) == 0:
        return
    m_word = word_list[0]
    cut_point = val_list.index(max(val_list))
    
    #return [s[:cut_point] ,  word_list[cut_point] , s[cut_point+2:]]
    left = cut_s(s[:cut_point])
    right = cut_s(s[cut_point+2:])
    if left: out_list.append(left)
    out_list.append(word_list[cut_point])

    '''
    for i in word_list[cut_point]:
        print i,
    print 
    '''
    if right: out_list.append(right)
    return  out_list

def view_words():
    for k in words.keys():
        print k
        for w1 in words[k].keys():
            print "\t", w1, words[k][w1]
        print '------------'
view_words()

def flatten(l, ltypes=(list)):
    i = 0
    while (i < len(l)):
       while (isinstance(l[i], ltypes)):
           l[i:i+1] = list(l[i])
       i += 1
    return l

def view_result(rs):
    rs = flatten(rs)
    for word in rs:
        for c in word:
            if c != 0:
                print c,
        print ' / ',
    print

def proc_s(s):
    learn_s(s)
    return cut_s(s)

###########################################################3
for line in text.split():
    learn_s(line.decode('utf-8'))
learn_s(u1)
learn_s(u2)
learn_s(u3)

print p1
rs = proc_s(p1)
view_result(rs)

print p2
rs = proc_s(p2.decode('utf-8'))
view_result(rs)

print p3
rs = proc_s(p3.decode('utf-8'))
view_result(rs)
